package com.xjtu.controller;

import com.xjtu.common.api.Result;
import com.xjtu.common.api.ResultCode;
import com.xjtu.model.Chat;
import com.xjtu.model.ChatType;
import com.xjtu.service.DemoService;
import com.xjtu.util.MinioUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@Api(tags = "接口demo")
@RequestMapping("/demo")
public class DemoController {

    @Resource
    private MinioUtil minioUtil;

    @Resource
    private DemoService demoService;


    @ApiOperation("文件上传demo")
    @PostMapping("upload")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "File", name = "file", value = "上传的图片", required = true),
            @ApiImplicitParam(dataType = "String", name = "bucketName", value = "对象存储桶名称", required = true)
    })
    public Object upload(@RequestParam("file") MultipartFile file,
                         @RequestParam("bucketName") String bucketName) {
        try {
            return minioUtil.uploadFile(bucketName, file);
        } catch (Exception e) {
            return new Result(ResultCode.ERROR);
        }
    }

    @ApiOperation("保存一条聊天记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "from", value = "发送方", example = "111", required = true),
            @ApiImplicitParam(name = "to", value = "接收方", example = "222", required = true)
    })
    @GetMapping("/add")
    public void newChat(@RequestParam("from") String from,
                        @RequestParam("to") String to) {
        Chat chat = new Chat(from, to, "测试记录", ChatType.TEXT);
        demoService.saveChat(chat);
    }


    @ApiOperation("查看全部聊天记录")
    @GetMapping("/get")
    public List<Chat> getAll() {
        return demoService.findAll();
    }
}
